unit settingform;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls,
  ComCtrls, StdCtrls, nkTitleBar, rxctrls, BCLabel, BCButton, Printers, IniFiles,
  LMessages, LCLIntf, EditBtn, Intf;

type

  { TfrmSetting }

  TfrmSetting = class(TForm)
    BCButton2: TBCButton;
    BCLabel1: TBCLabel;
    ComboBox1: TComboBox;
    Edit1: TEdit;
    fileedit: TFileNameEdit;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    nkTitleBar2: TnkTitleBar;
    PageControl1: TPageControl;
    Panel1: TPanel;
    RxSpeedButton1: TRxSpeedButton;
    TabSheet1: TTabSheet;
    procedure BCButton2Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure RxSpeedButton1Click(Sender: TObject);
  private

  public

  end;

var
  frmSetting: TfrmSetting;

implementation

uses
  datamodule;
{$R *.frm}

{ TfrmSetting }

procedure TfrmSetting.FormClose(Sender: TObject; var CloseAction: TCloseAction);
begin
  CloseAction:=caFree;
end;

procedure TfrmSetting.BCButton2Click(Sender: TObject);
var
  mIni:TIniFile;
begin
  if Trim(Edit1.Text)='' then
  begin
    MessageDlg('请输入机台编号。',mtWarning,[mbOK],0);
    Edit1.SetFocus;
    Exit;
  end;
  if Trim(ComboBox1.Text)='' then
  begin
    MessageDlg('请选择要使用的打印机。',mtWarning,[mbOK],0);
    ComboBox1.SetFocus;
    Exit;
  end;
  if Trim(FileEdit.Text)='' then
  begin
    MessageDlg('请选择打印模板。',mtWarning,[mbOK],0);
    FileEdit.SetFocus;
    Exit;
  end;
  Dm.PrinterID:=Trim(Edit1.Text);
  Dm.PrinterName:=Trim(ComboBox1.Text);
  Dm.PrintTemplate:=Trim(FileEdit.Text);
  //保存设置
  mIni:=TIniFile.Create(ExtractFilepath(Application.ExeName)+'custom.ini');
  mIni.WriteString('cfprint','name',Dm.PrinterName);
  mIni.WriteString('cfprint','id',Dm.PrinterID);
  mIni.WriteString('cfprint','tpl',Dm.PrintTemplate);
  mIni.Free;
  //向主窗口发送通知消息
  SendMessage(Application.MainFormHandle,MSG_CONFIG_CHANGED,0,0);
  Close;
end;

procedure TfrmSetting.FormCreate(Sender: TObject);
var
  mPrinter:string;
begin
  ComboBox1.Items:=Printer.Printers;
  mPrinter:=Dm.PrinterName;
  Edit1.Text:=Dm.PrinterID;
  if Trim(mPrinter)<>'' then
  begin
    ComboBox1.ItemIndex:=ComboBox1.Items.IndexOf(mPrinter);
  end;
  FileEdit.InitialDir:=ExtractFilepath(Application.ExeName);
  FileEdit.Text:=Dm.PrintTemplate;
end;

procedure TfrmSetting.FormDestroy(Sender: TObject);
begin
  frmSetting:=nil;
end;

procedure TfrmSetting.RxSpeedButton1Click(Sender: TObject);
begin
  Close;
end;

end.

